package com.ssm.test;

import com.ssm.dao.UserDao;
import com.ssm.mapper.UserMapper;
import com.ssm.po.User;
import com.ssm.po.UserExample;
import com.ssm.routerweb.DataSourceContextHolder;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.testng.annotations.Test;

import java.util.List;

/**
 * Created by Mars-Hasee on 2017/8/3.
 */
public class test {

  ApplicationContext  applicationContext = new ClassPathXmlApplicationContext("spring/applicationContext.xml");
    UserMapper userMapper = (UserMapper) applicationContext.getBean("userMapper");

    @Test
    public void findOneDao() throws Exception {
        UserDao userDao = (UserDao) applicationContext.getBean("userDao");
        User user = userDao.findOne(1);
        System.out.println(user);
    }

        //根据主键查询
    @Test
    public void findOneMapper() throws Exception {
      User user = userMapper.selectByPrimaryKey(1);
        System.out.println(user);
    }


    //自定义条件查询
    @Test
    public void findByExample() throws Exception {
        UserExample userExample = new UserExample();
        UserExample.Criteria criteria = userExample.createCriteria();
        criteria.andAddressEqualTo("湖北省");
        List<User> user = userMapper.selectByExample(userExample);
        System.out.println(user);
    }

    //插入
    @Test
    public void insert() throws Exception {
        //支持多数据源
        DataSourceContextHolder.setDbType("dataSource2");
        User user = new User();
      user.setAddress("上海市");
      user.setName("天下无双");
        userMapper.insert(user);
    }
    //更新
    @Test
    public void update () throws Exception {
        DataSourceContextHolder.setDbType("dataSource2");
        //对所有的字段进行更新，先查询出来在更新
        User user = userMapper.selectByPrimaryKey(1);
        user.setName("梅赛德斯");
        userMapper.updateByPrimaryKey(user);
        //对传入的非空字段更新，不用先查出来，适用于批量更新
//        userMapper.updateByExampleSelective();
    }



}
